package com.ccnu.demojava.net.bio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

public class RBioClient {
	public static void send() {
		try {
			Socket s = new Socket("192.168.0.11", 8060);
//			String sMsg = "Hello World!\n";
			String sMsg = "00000761<?xml version=\"1.0\" encoding=\"UTF-8\"?><service><sys-header><data name=\"SYS_HEAD\"><struct><data name=\"SOURCE_TYPE\"><field type=\"string\" length=\"2\" scale=\"0\">03</field></data><data name=\"SERVICE_CODE\"><field type=\"string\" length=\"2\" scale=\"0\">1001</field></data>		<data name=\"SERVICE_SCENE\"><field type=\"string\" length=\"2\" scale=\"0\"></field></data>						</struct></data></sys-header><app-header><data name=\"APP_HEAD\"><struct><data name=\"TOTAL_FLAG\"><field type=\"string\" length=\"1\" scale=\"0\">E</field></data></struct></data></app-header><local-header><data name=\"LOCAL_HEAD\"><struct></struct></data></local-header><body><data name=\"BODY\"><struct><data name=\"TEST\"><field type=\"string\" length=\"30\" scale=\"0\">林瞈治</field></data></struct></data></body></service>";
			OutputStream os = s.getOutputStream();
			while (true) {
				if (s.isConnected()) {
					os.write(sMsg.getBytes());
					break;
				}
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}

			BufferedReader is = new BufferedReader(new InputStreamReader(s.getInputStream()));
			while (true) {
				if (s.isConnected()) {
					String rspMsg = is.readLine();
					if (rspMsg != null) {
						System.out.println(rspMsg);
						break;
					}
				}
			}
			os.close();
			s.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
